import React, { Fragment } from "react";
import { Route, Redirect, Switch } from "react-router-dom";

export default function Index({ routes }) {
  const routesList = routes.filter((item) => !item.redirect);
  const redirectList = routes.filter((item) => item.redirect);
  return (
    <Switch>
      {routesList.map((item, index) => {
        return  <Route
            path={item.path}
            render={() => {
              let Com = item.component;
              if (item.auth && !sessionStorage.getItem("token")) {
                console.log(item);
                return <Redirect
                    to={{
                      pathname: "/login",
                      backUrl: item.path
                    }}
                  />
                
              } else {
                return <Com children={item.children} />;
              }
            }}
            key={index}/>
        
      })}

      {
        redirectList.map((item, index) => {
        return <Redirect to={item.redirect} key={index} />;
      })
      }
    </Switch>
  );
}
